#![allow(unused_imports,unused_assignments)]
use serde::{Deserialize, Serialize};
use sqlx::mysql::MySqlRow;
use sqlx::{FromRow, Row};
use sqlx::Error;
use crate::domain::count_num::get_image_base64;
use crate::domain::count_num::get_i64;
use crate::domain::count_num::get_i32;
use crate::domain::count_num::get_f64;
use crate::domain::count_num::get_f32;
use crate::domain::count_num::get_bool;
use crate::domain::count_num::get_string;
use crate::domain::count_num::get_dropdown;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GoodsBean {
	pub id: i64,
	pub name: String,
	pub active: bool,
	pub cover: String,
	pub image1: String,
	pub image2: String,
	pub price: f64,
	pub intro: String,
	pub stock: i32,
	pub goods_type_id: i64,
	pub is_scroll: bool,
	pub is_hot: bool,
	pub is_new: bool,
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GoodsQueryRequest {
	pub id: Option<String>,
	pub name: Option<String>,
	pub active: Option<String>,
	pub cover: Option<String>,
	pub image1: Option<String>,
	pub image2: Option<String>,
	pub price: Option<String>,
	pub intro: Option<String>,
	pub stock: Option<String>,
	pub goods_type_id: Option<String>,
	pub is_scroll: Option<String>,
	pub is_hot: Option<String>,
	pub is_new: Option<String>,
	pub page:Option<i32>,
	pub rows:Option<i32>,
}

impl<'c> FromRow<'c, MySqlRow> for GoodsBean {
	fn from_row(row: &MySqlRow) -> Result<Self, Error> {
		Ok(GoodsBean {
			id: row.get(0),
			name: row.get(1),
			active: row.get(2),
			cover:get_image_base64(row.get(3)),
			image1:get_image_base64(row.get(4)),
			image2:get_image_base64(row.get(5)),
			price:get_f64(row.get(6)),
			intro:get_string(row.get(7)),
			stock:get_i32(row.get(8)),
			goods_type_id:get_dropdown(row.get(9)),
			is_scroll:get_bool(row.get(10)),
			is_hot:get_bool(row.get(11)),
			is_new:get_bool(row.get(12)),
		})
	}
}

impl PartialEq for GoodsBean {
	fn eq(&self, other: &Self) -> bool {
		self.id == other.id
	}
}
